package edu.hawaii.myisern.commandline;

import static org.junit.Assert.assertEquals;
import java.io.File;
import org.junit.Test;

/**
 * Demonstrates that the MyIsernQueries class is working. 
 * 
 * @author Phuoc Le
 * @author Ka Yee Leung
 * @author Jared Sunouchi
 */
public class TestMyIsernQueries {
  
  // sample arguments used for testing
  private String organizationValue = "-organization";
  private String researcherValue = "-researcher";
  private String collaborationValue = "-collaboration";
  private String describeValue = "-describe";
  private String listCollaborationValue = "-listCollaborations";
  private String listOrganizationValue = "-listOrganizations";
  private String allValue = "-all";
  private String researcher = "Philip_Johnson";
  private String listCollaborationArg = "-collaborationLevelGreaterThan";
  private String loadTest = "-loadTest";
  private String uh = "University_of_Hawaii";
  private String collabArg = "-collaborationLevelEquals";
  
  private String userDirectory = "user.dir";
  private File validCollaborationFile = new File(System.getProperty(userDirectory)
      + "/xml/tests/test.collaborations.example.xml");
  private File validOrganizationFile = new File(System.getProperty(userDirectory)
      + "/xml/tests/test.organizations.example.xml");
  private File validResearcherFile = new File(System.getProperty(userDirectory)
      + "/xml/tests/test.researchers.example.xml");
  
  /**
   * Tests the methods of MyIsernQueries and tests MyIsernCommandLineInterface. 
   * @throws Exception If problems occur.
   */
  @Test public void testQueries() throws Exception {
    MyIsernXmlLoader loader = new MyIsernXmlLoader(validCollaborationFile, validOrganizationFile, 
        validResearcherFile);
    loader.sanityCheck();
    MyIsernQueries queries = new MyIsernQueries(loader); 
  
    //  test the -describe task
    String testArgs1[] = { describeValue, allValue, "Researchers", loadTest };
    String testArgs2[] = { describeValue, allValue, "Organizations", loadTest };
    String testArgs3[] = { describeValue, allValue, "Collaborations", loadTest };
    String testArgs4[] = { describeValue, researcherValue, "Victor_Basili", loadTest };
    String testArgs5[] = { describeValue, organizationValue, "University_of_Maryland", loadTest };
    String testArgs6[] = { describeValue, collaborationValue, "UM-UH-HPCS", loadTest };

    MyIsernCommandLineInterface.main(testArgs1);
    MyIsernCommandLineInterface.main(testArgs2);
    MyIsernCommandLineInterface.main(testArgs3);
    MyIsernCommandLineInterface.main(testArgs4);
    MyIsernCommandLineInterface.main(testArgs5);
    MyIsernCommandLineInterface.main(testArgs6);

    // assure generateDescription is printing out the correct descriptions.
    assertEquals("Check describe task researcher", "Philip Johnson is a Professor of " +
        "Information and Computer Sciences at the University of Hawaii.", 
        queries.generateDescription(researcher, researcherValue));
    assertEquals("Check describe task researcher", null, 
        queries.generateDescription("Not_Valid", researcherValue));
    assertEquals("Check describe task organization", "The Collaborative Software Development " +
        "Laboratory does research on software measurement.", 
        queries.generateDescription(uh, organizationValue));
    assertEquals("Check describe task organization", null, 
        queries.generateDescription("Not_Valid_University", organizationValue));
    assertEquals("Check describe task collaboration", "A graduate student at the University " +
        "of Hawaii spent the summer working in the University of Maryland research group.", 
        queries.generateDescription("UM-UH-HPCS", collaborationValue));
    assertEquals("Check describe task collaboration", null, 
        queries.generateDescription("Not_Valid_Collaboration", collaborationValue));
    
    // validate listCollaborations()
    assertEquals("Check listCollaboration table size for year", 1, 
        queries.listCollaborations("2005", "-year").getRowCount());
    assertEquals("Check listCollaboration table size for year", null, 
        queries.listCollaborations("1999", "-year"));
    assertEquals("Check listCollaboration table size for researcher", 1, 
        queries.listCollaborations(researcher, researcherValue).getRowCount());
    assertEquals("Check listCollaboration table size for researcher", null, 
        queries.listCollaborations("Not_Valid_ID", researcherValue));
    assertEquals("Check listCollaboration table size for organization", 1, 
        queries.listCollaborations(uh, organizationValue).getRowCount());
    assertEquals("Check listCollaboration table size for organization", null, 
        queries.listCollaborations("University_of_None", organizationValue));

    queries.createJPanel(queries.listCollaborations(uh, organizationValue));
    
    String testArgs8[] = { listCollaborationValue, organizationValue, uh, 
        loadTest };
    String testArgs9[] = { listCollaborationValue, researcherValue, researcher, loadTest };

    MyIsernCommandLineInterface.main(testArgs8);
    MyIsernCommandLineInterface.main(testArgs9);

    // verifies that listOrganizations is returning the correct organizations
    StringBuffer expectedValue = new StringBuffer();
    String value = "University of Hawaii";
    expectedValue.append(value);
    value = "\n";
    expectedValue.append(value);
    value = "University of Maryland";
    expectedValue.append(value);
    value = "\n";
    expectedValue.append(value);
    assertEquals("Check listOrganization task GreaterThan", expectedValue.toString(), 
        queries.listOrganizations(listCollaborationArg, 0));
    assertEquals("Check listOrganization task Equals", expectedValue.toString(), 
        queries.listOrganizations(collabArg, 1));
    assertEquals("Check listOrganization task GreaterThan", "", 
        queries.listOrganizations(listCollaborationArg, 1));
    assertEquals("Check listOrganization task Equals", "", 
        queries.listOrganizations(collabArg, 2));

    String testArgs10[] = { listOrganizationValue, collabArg, "1", loadTest };
    String testArgs11[] = { listOrganizationValue, listCollaborationArg, "2", loadTest };

    MyIsernCommandLineInterface.main(testArgs10);
    MyIsernCommandLineInterface.main(testArgs11);
    
    // test the display all tables feature
    String testArgs12[] = { "-display", loadTest };
    
    MyIsernCommandLineInterface.main(testArgs12);
  }
}
